package org.jeecg.modules.demo.live.webControllser;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.query.QueryRuleEnum;
import org.jeecg.config.shiro.IgnoreAuth;
import org.jeecg.modules.demo.live.entity.JkLive;
import org.jeecg.modules.demo.live.service.IJkLiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Tag(name="jk_live前端")
@RestController
@RequestMapping("/api/live")
@Slf4j
public class JKWebLiveController {

    @Autowired
    private IJkLiveService jkLiveService;

    /**
     * 分页列表查询
     *
     * @param jkLive
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "jk_live-分页列表查询")
    @Operation(summary="直播-分页列表查询")
    @GetMapping(value = "/list")
    @IgnoreAuth
    public Result<IPage<JkLive>> queryPageList(JkLive jkLive,
                                               @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                               @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                               HttpServletRequest req) {

        QueryWrapper<JkLive> queryWrapper = QueryGenerator.initQueryWrapper(jkLive, req.getParameterMap());
        queryWrapper.eq("is_enabled", "Y");
        Page<JkLive> page = new Page<JkLive>(pageNo, pageSize);
        IPage<JkLive> pageList = jkLiveService.page(page, queryWrapper);
        //jkLiveService.list();
        return Result.OK(pageList);
    }

    @Operation(summary="直播-我的直播:未开始及直播中")
    @GetMapping(value = "/listmy")
    @IgnoreAuth
    public Result<IPage<JkLive>> queryPageListmy(JkLive jkLive,
                                               @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                               @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                               HttpServletRequest req) {

        QueryWrapper<JkLive> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_enabled", "Y");
        queryWrapper.eq("main_id", jkLive.getMainId());
        queryWrapper.ne("state", "3");
        Page<JkLive> page = new Page<JkLive>(pageNo, pageSize);
        IPage<JkLive> pageList = jkLiveService.page(page, queryWrapper);
        //jkLiveService.list();
        return Result.OK(pageList);
    }
    @Operation(summary="直播-观看直播:未开始及直播中")
    @GetMapping(value = "/listother")
    @IgnoreAuth
    public Result<IPage<JkLive>> queryPageListother(JkLive jkLive,
                                                 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                 HttpServletRequest req) {

        QueryWrapper<JkLive> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_enabled", "Y");
        queryWrapper.ne("main_id", jkLive.getMainId());
        queryWrapper.eq("state", "2");
        Page<JkLive> page = new Page<JkLive>(pageNo, pageSize);
        IPage<JkLive> pageList = jkLiveService.page(page, queryWrapper);
        //jkLiveService.list();
        return Result.OK(pageList);
    }


    @AutoLog(value = "jk_live-编辑")
    @Operation(summary="jk_live-编辑")
    @IgnoreAuth
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
    public Result<String> edit(@RequestParam(name="id") Integer id,
                               @RequestParam(name="status") String status) {
        JkLive entity = new JkLive();
        entity.setId(id);           // 设置主键ID
        entity.setState(status);   // 设置需要更新的状态字段

         // 2. 调用MyBatis-Plus的updateById方法
         jkLiveService.updateById(entity);
        return Result.OK("编辑成功!");
    }
}
